/* This file is part of the Crimsoon project
  * Copyright (C) 2009 Eduardo Henrique <eduardohleite@gmail.com>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
  * License as published by the Free Software Foundation; either
  * version 2 of the License, or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Library General Public License for more details.
  *
  * You should have received a copy of the GNU Library General Public License
  * along with this library; see the file COPYING.LIB.  If not, write to
  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  * Boston, MA 02110-1301, USA.
*/

#include <QSqlQueryModel>
#include <QSqlQuery>
#include <QHeaderView>
#include <QModelIndex>
#include <QAbstractTableModel>

#include <QDebug>
#include <QSqlError>

#include "gallerylist.h"
#include "gallery.h"
#include "playlistmodel.h"
#include "playlist.h"
#include "track.h"

void GalleryList::adjustSizes()
{
    this->setColumnWidth(0, 30);
    this->setColumnWidth(1, 250);
    this->setColumnWidth(2, 150);
    this->setColumnWidth(3, 250);
    this->setColumnWidth(4, 80);
    this->setColumnHidden(5, true);
}

void GalleryList::refresh()
{
    Gallery gallery;
    refresh(gallery.getQueryAllFiles());
}

void GalleryList::refresh(QString artist)
{
    Gallery gallery;
    refresh(gallery.getQueryArtistFiles(artist));
}

void GalleryList::refresh(QString artist, QString album)
{
    Gallery gallery;
    refresh(gallery.getQueryAlbumFiles(artist, album));
}

void GalleryList::refresh(QSqlQuery query)
{    
    sqlModel->setQuery(query);
    model = sqlModel;
    this->setModel(model);
    adjustSizes();
}

void GalleryList::refresh(PlayList *playlist)
{    
    playModel->setPlayList(playlist);
    model = playModel;
    this->setModel(model);

    adjustSizes();
}

QString GalleryList::getPathAt(qint32 row)
{
    QModelIndex pathIndex = sqlModel->index(row, 5);
    return pathIndex.data().toString();
}

qint32 GalleryList::isShowingPath(QString path)
{
    for (qint32 i=0;i<model->rowCount();i++)
    {
        if (model->index(i, 5).data().toString() == path)
        {
            return i;
        }
    }

    return -1;
}

PlayList GalleryList::getPlayList()
{
    PlayList list;
    Track track;

    for (qint32 i=0;i<model->rowCount();i++)
    {
        track.setTrack   (model->index(i, 0).data().toInt());
        track.setTitle   (model->index(i, 1).data().toString());
        track.setArtist  (model->index(i, 2).data().toString());
        track.setAlbum   (model->index(i, 3).data().toString());
        track.setLength  (model->index(i, 4).data(Qt::EditRole).toInt());
        track.setFileName(model->index(i, 5).data().toString());

        list.append(track);
    }

    return list;
}

GalleryList::GalleryList()
{
    sqlModel  = new PlayListQueryModel(this);
    playModel = new PlayListModel(this);

    model = sqlModel;

    this->setModel(model);
    this->verticalHeader()->hide();
    this->setAlternatingRowColors(true);

    adjustSizes();
}

GalleryList::~GalleryList()
{
    delete sqlModel;
    delete playModel;
}
